package com.xmall.order.service.impl.platform;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmall.define.dict.OrderStatusEnum;
import com.xmall.order.define.dto.platform.OrderDtoForAdmin;
import com.xmall.order.define.dto.platform.OrderOperateBtnForAdmin;
import com.xmall.order.define.request.query.OrderListPageQueryRequest;
import com.xmall.order.entity.Order;
import com.xmall.order.service.IOrderRefundService;
import com.xmall.order.service.IOrderService;
import com.xmall.order.service.platform.IOrderAdminService;
import com.xmall.order.utils.ConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderAdminServiceImpl implements IOrderAdminService {

    @Resource
    private IOrderService orderService;

    @Resource
    private IOrderRefundService orderRefundService;

    /**
     * 分页列表
     */
    @Override
    public Page<OrderDtoForAdmin> listPage(OrderListPageQueryRequest request) {
        Page<OrderDtoForAdmin> result = new Page<>();

        Page<Order> page = orderService.page(new Page(request.getPage(), request.getPageSize()), this.fillQueryParams(request));
        BeanUtils.copyProperties(page, result);
        result.setRecords(ConvertUtils.OrderModule.INSTANCE.listToAdmin(page.getRecords()));

        result.getRecords().stream().forEach(orderDto -> orderDto.setHasUnhandledRefund(orderRefundService.hasUnhandledRefund(orderDto.getId())));
        result.getRecords().stream().forEach(orderDto -> this.setOrderOperateBtnState(orderDto));
        return result;
    }

    /**
     * 查询条件
     */
    private QueryWrapper<Order> fillQueryParams(OrderListPageQueryRequest request) {
        QueryWrapper<Order> query = new QueryWrapper<>();

        query.orderByDesc("create_time");
        query.like(StrUtil.isNotBlank(request.getMemberName()), "member_name", request.getMemberName());
        query.eq(null != request.getOrderSn(), "order_sn", request.getOrderSn());
        query.ne(null != request.getOrderStatus() && -1 == request.getOrderStatus(), "order_status", OrderStatusEnum.CANCELED.getStatus());
        query.eq(null != request.getOrderStatus() && -1 != request.getOrderStatus(), "order_status", request.getOrderStatus());
        query.eq(null != request.getPayType(), "pay_type", request.getPayType());
        query.eq(null != request.getPayPlatform(), "pay_platform", request.getPayPlatform());
        return query;
    }

    private void setOrderOperateBtnState(OrderDtoForAdmin orderDto) {
        Integer orderStatus = orderDto.getOrderStatus();
        OrderOperateBtnForAdmin operateBtnStatus = new OrderOperateBtnForAdmin();

        // 发货按钮
        operateBtnStatus.setShowConsignBtn(OrderStatusEnum.WAIT_CONSIGN.getStatus() == orderStatus && !orderDto.getHasUnhandledRefund());
        // 售前处理退款按钮
        operateBtnStatus.setShowHandleRefundBtn(OrderStatusEnum.WAIT_CONSIGN.getStatus() == orderStatus && orderDto.getHasUnhandledRefund());
        // 售后处理退款按钮
        operateBtnStatus.setShowHandleReturnBtn((OrderStatusEnum.WAIT_RECEIVE.getStatus() == orderStatus ||
                OrderStatusEnum.FINISHED.getStatus() == orderStatus ||
                OrderStatusEnum.PART_REFUND.getStatus() == orderStatus)
                && orderDto.getHasUnhandledRefund());
        // 编辑快递信息按钮
        operateBtnStatus.setUpdateDeliveryBtn(OrderStatusEnum.WAIT_RECEIVE.getStatus() == orderStatus);
        orderDto.setOperateBtn(operateBtnStatus);
    }

}
